package com.xxh.cloud.provider.listener;

import com.xxh.cloud.common.config.ContentConfig;
import com.xxh.cloud.common.util.StringUtil;
import com.xxh.cloud.provider.config.RabbitMQConfig;
import com.xxh.cloud.provider.entry.User;
import com.xxh.cloud.provider.entry.UserLog;
import com.xxh.cloud.provider.mapper.UserLogMapper;
import com.xxh.cloud.provider.mapper.UserMapper;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author ASUS
 * @date 2021/9/4 21:23
 */
@Component
@RabbitListener(queues = RabbitMQConfig.QUEUE_USER_ADD)  //监听哪个消息队列
public class UserAddListener {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserLogMapper userLogMapper;

    @RabbitHandler
    @Transactional  //开启事务
    public void handler(String msg){
        //效验消息
        if (!StringUtil.isEmpty(msg)){
            //查询数据库  根据监听到的手机号
            User user = userMapper.selectByPhone(msg);
            if (user == null){
                //实现自动注册
                user = new User();
                user.setPhone(msg);
                user.setFlag(1);  //  1 新增  2 账号密码 。。。。。。
                if (userMapper.insert(user)>0){
                    userLogMapper.insert(new UserLog(user.getId(), ContentConfig.USER_LOG_ADD,"新用户注册-验证登陆下的自动注册",new Date(),""));
                }

            }
        }
    }





}
